package com.example.chapter.service.impl;

import com.example.chapter.entity.User;
import com.example.chapter.mapper.UserMapper;
import com.example.chapter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    UserMapper userMapper;
    @Override
    public int addUser(User user) {
       String sql ="insert into user (username,password,sex) values (?,?,?)";
       return jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getSex());
    }

    public String login(String username, String password) {
        // 查询用户
        User user = userMapper.selectByUsername(username);
        if (user != null && user.getPassword().equals(password)) {

            return "Login successful";
        } else {
            return "Invalid username or password";
        }
    }
    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User findUser(int id) {
        return userMapper.selectById(id);
    }

}
